package v1

import (
	"gitee.com/wochong/HalloGin/model"
	"gitee.com/wochong/HalloGin/package/app"
	"gitee.com/wochong/HalloGin/package/e"
	"gitee.com/wochong/HalloGin/package/util"
	"github.com/gin-gonic/gin"
	"github.com/golang/glog"
	"net/http"
)

/**
 * @api {get} /user/:model_id 获取用户
 * @apiName GetUser
 * @apiGroup group
 *
 * @apiParam {Number} id Users unique ID.
 *
 * @apiSuccess {String} firstname Firstname of the User.
 *
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "firstname": "John",
 *       "lastname": "Doe"
 *     }
 *
 * @apiError UserNotFound The id of the User was not found.
 *
 * @apiErrorExample Error-Response:
 *     HTTP/1.1 404 Not Found
 *     {
 *       "error": "UserNotFound"
 *     }
*/
// GetUser 获取用户 /user/:model_id
func GetUser(c *gin.Context) {

	appGin := app.Gin{C: c}

	glog.V(2).Infoln("getUser")

	// verify
	data := &struct {
		UserID uint `json:"user_id"`
	}{}

	err := c.ShouldBindJSON(data)
	if err != nil {
		glog.Error(err)
		appGin.Response(http.StatusBadRequest, e.ERROR)
		return
	}

	// get
	user, err := model.GetUser(data.UserID)
	if err != nil {
		glog.Error(err)
		appGin.Response(http.StatusBadRequest, e.ERROR)
		return
	}

	appGin.Response(http.StatusOK, e.SUCCESS, user)
}

/**
 * @api {get} /users 获取所有用户
 * @apiName GetAllUser
 * @apiGroup group
 *
 * @apiParam {Number} id Users unique ID.
 *
 * @apiSuccess {String} firstname Firstname of the User.
 *
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "firstname": "John",
 *       "lastname": "Doe"
 *     }
 *
 * @apiError UserNotFound The id of the User was not found.
 *
 * @apiErrorExample Error-Response:
 *     HTTP/1.1 404 Not Found
 *     {
 *       "error": "UserNotFound"
 *     }
*/
// GetAllUser 获取所有用户 /users
func GetAllUser(c *gin.Context) {

	appGin := app.Gin{C: c}

	glog.V(2).Infoln("getAllUser")

	// get
	users, err := model.GetAllUser()
	if err != nil {
		glog.Error(err)
		appGin.Response(http.StatusBadRequest, e.ERROR)
		return
	}

	appGin.Response(http.StatusOK, e.SUCCESS, users)
}

type AddUserJson struct {
	Name     string `json:"name" gorm:"" binding:"required"`
	Phone    int    `json:"phone" gorm:"" binding:"required"`
	Password string `json:"password" gorm:""  binding:"required"`
	Email    string `json:"email" gorm:"" binding:"required"`
	ParentID uint   `json:"parent_id" gorm:""`
}

/**
 * @api {get} /user 添加用户
 * @apiName AddUser
 * @apiGroup group
 *
 * @apiParam {Number} id Users unique ID.
 *
 * @apiSuccess {String} firstname Firstname of the User.
 *
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "firstname": "John",
 *       "lastname": "Doe"
 *     }
 *
 * @apiError UserNotFound The id of the User was not found.
 *
 * @apiErrorExample Error-Response:
 *     HTTP/1.1 404 Not Found
 *     {
 *       "error": "UserNotFound"
 *     }
*/
// AddUser 添加用户 /user
func AddUser(c *gin.Context) {

	appGin := app.Gin{C: c}

	glog.V(2).Infoln("addUser")

	data := &AddUserJson{}

	err := c.ShouldBindJSON(data)
	if err != nil {
		glog.Error(err)
		appGin.Response(http.StatusBadRequest, e.INVALID_PARAMS)
		return
	}

	user := model.User{
		Name:     data.Name,
		Phone:    data.Phone,
		Password: util.HashString(data.Password),
		Email:    data.Email,
		ParentID: data.ParentID,
	}

	err = user.Add()
	if err != nil {
		glog.Error(err)
		appGin.Response(http.StatusBadRequest, e.ERROR_ADD_USER_FAIL)
		return
	}

	appGin.Response(http.StatusCreated, e.SUCCESS)
}

/**
 * @api {get} /user 更新用户
 * @apiName UpdateUser
 * @apiGroup group
 *
 * @apiParam {Number} id Users unique ID.
 *
 * @apiSuccess {String} firstname Firstname of the User.
 *
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "firstname": "John",
 *       "lastname": "Doe"
 *     }
 *
 * @apiError UserNotFound The id of the User was not found.
 *
 * @apiErrorExample Error-Response:
 *     HTTP/1.1 404 Not Found
 *     {
 *       "error": "UserNotFound"
 *     }
*/
// UpdateUser 更新用户 /user
func UpdateUser(c *gin.Context) {

	appGin := app.Gin{C: c}

	glog.V(2).Infoln("UpdateUser")

	data := &struct {
		ID       uint   `gorm:"primary_key" binding:"required"`
		Name     string `json:"name" gorm:""`
		Phone    int    `json:"phone" gorm:""`
		Password string `json:"password" gorm:""`
		Email    string `json:"email" gorm:""`
		ParentID uint   `json:"parent_id" gorm:""`
	}{}

	err := c.ShouldBindJSON(data)
	if err != nil {
		glog.Error(err)
		appGin.Response(http.StatusBadRequest, e.INVALID_PARAMS)
		return
	}

	user, err := model.GetUser(data.ID)
	if err != nil {
		glog.Error(err)
		appGin.Response(http.StatusBadRequest, e.INVALID_PARAMS)
	}

	upUser := &model.User{
		Name:     data.Name,
		Phone:    data.Phone,
		Password: data.Password,
		Email:    data.Email,
		ParentID: data.ParentID,
	}

	err = user.Updates(upUser)
	if err != nil {
		glog.Error(err)
		appGin.Response(http.StatusBadRequest, e.INVALID_PARAMS)
		return
	}

	appGin.Response(http.StatusCreated, e.SUCCESS)
}


/**
 * @api {get} /userV2 更新用户
 * @apiName UpdateUserV2
 * @apiGroup group
 *
 * @apiParam {Number} id Users unique ID.
 *
 * @apiSuccess {String} firstname Firstname of the User.
 *
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "firstname": "John",
 *       "lastname": "Doe"
 *     }
 *
 * @apiError UserNotFound The id of the User was not found.
 *
 * @apiErrorExample Error-Response:
 *     HTTP/1.1 404 Not Found
 *     {
 *       "error": "UserNotFound"
 *     }
*/
// UpdateUserV2 更新用户 /userV2
func UpdateUserV2(c *gin.Context) {

	appGin := app.Gin{C: c}

	glog.V(2).Infoln("UpdateUser")

	data := &struct {
		ID       uint   `gorm:"primary_key" binding:"required"`
		Name     string `json:"name" gorm:""`
		Phone    int    `json:"phone" gorm:""`
		Password string `json:"password" gorm:""`
		Email    string `json:"email" gorm:""`
		ParentID uint   `json:"parent_id" gorm:""`
	}{}

	err := c.ShouldBindJSON(data)
	if err != nil {
		glog.Error(err)
		appGin.Response(http.StatusBadRequest, e.INVALID_PARAMS)
		return
	}

	user := &model.User{
		Name:     data.Name,
		Phone:    data.Phone,
		Password: data.Password,
		Email:    data.Email,
		ParentID: data.ParentID,
	}

	err = model.UpdateUser(data.ID, user)
	if err != nil {
		glog.Error(err)
		appGin.Response(http.StatusBadRequest, e.INVALID_PARAMS)
		return
	}

	appGin.Response(http.StatusCreated, e.SUCCESS)
}
